package com.tool.hooker_inject;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    EditText et_aid;
    EditText et_cid;

    Switch sh_toggle;
    Button bt_save;

    String aid;
    String cid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

        final boolean[] straight = {false};
        sh_toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                straight[0] = isChecked;
                Toast.makeText(MainActivity.this, isChecked+"", Toast.LENGTH_SHORT).show();
            }
        });

        bt_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    JSONObject jsonObject = new JSONObject();

                    Editable aidText = et_aid.getText();
                    if (!TextUtils.isEmpty(aidText)) {
                        jsonObject.put("aid", aidText.toString());
                    }
                    Editable etCidText = et_cid.getText();
                    if (!TextUtils.isEmpty(etCidText)) {
                        jsonObject.put("cid", etCidText.toString());
                    }

                    jsonObject.put("qt",straight[0]);

                    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + ".omgtest" + File.separator + "inject.txt";

                    if (jsonObject.length() > 0) {
                        boolean result = FileManager.writeString(path, jsonObject.toString());

                        String msg = result ? "suc" : "fail";

                        Toast.makeText(v.getContext(), msg, Toast.LENGTH_SHORT).show();
                    }else {
                        File file = new File(path);
                        file.delete();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


    }

    private void init() {
        et_aid = findViewById(R.id.et_aid);
        et_cid = findViewById(R.id.et_cid);

        sh_toggle = findViewById(R.id.sh_toggle);
        bt_save = findViewById(R.id.bt_save);
    }
}
